#if UNITY_EDITOR
using Net.Helper;
using Net.Unity;
using Net.UnityComponent;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(PrefabCollection), editorForChildClasses: true)]
[CanEditMultipleObjects]
public class PrefabCollectionEditor : Editor
{
    private PrefabCollection prefabCollection;

    private void OnEnable()
    {
        prefabCollection = target as PrefabCollection;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        if (GUILayout.Button("收集网络预制体"))
        {
            prefabCollection.prefabInfos.Clear();
            var guids = AssetDatabase.FindAssets("t:Prefab");
            foreach (string guid in guids)
            {
                var path = AssetDatabase.GUIDToAssetPath(guid);
                var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                var components = prefab.GetComponents<MonoBehaviour>();
                foreach (var component in components)
                {
                    if (component == null)
                        continue;
                    var typeName = component.GetType().ToString();
                    if (typeName == "Net.UnityComponent.NetworkObject" || typeName == "ActorSystem.ActorView")
                    {
                        prefabCollection.prefabInfos.Add(new PrefabInfo()
                        {
                            prefabPath = path,
                            prefabHash = path.CRC32(),
                            prefab = prefab,
                        });
                        break;
                    }
                }
            }
            EditorUtility.SetDirty(prefabCollection);
        }
    }
}
#endif